package com.aier.user.entity.userapp;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.common.common.SystemDateFormatUtil;

public class UserModuleRowMapper implements RowMapper<UserModule>{
    private int type;
    public static final int AND_DATESTR = 1;

    public UserModuleRowMapper() {
    }

    public UserModuleRowMapper(int type) {
        this.type = type;
    }

    @Override
    public UserModule mapRow(ResultSet rs, int rowNum) throws SQLException {
        UserModule um = null;
        switch (type) {
            case AND_DATESTR:
                um = parseUserModule(new UserModule(), rs, "um");
                um.setSdate_str(SystemDateFormatUtil.formatDate(um.getSdate(), SystemDateFormatUtil.FORMAT_DATE));
                um.setEdate_str(SystemDateFormatUtil.formatDate(um.getEdate(), SystemDateFormatUtil.FORMAT_DATE));
                break;
            default:
                um = parseUserModule(new UserModule(), rs);
        }
        return um;
    }

    public static UserModule parseUserModule(UserModule s, ResultSet rs) throws SQLException {
        return parseUserModule(s, rs, null);
    }

    public static UserModule parseUserModule(UserModule s, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            s.setUmid(rs.getLong(su+"umid"));
        } catch (SQLException e) {
            s.setUmid(null);
        }
        try {
            s.setUid(rs.getLong(su+"uid"));
        } catch (SQLException e) {
            s.setUid(null);
        }
        try {
            s.setMid(rs.getLong(su+"mid"));
        } catch (SQLException e) {
            s.setMid(null);
        }
        try {
            s.setSdate(rs.getDate(su+"sdate"));
        } catch (SQLException e) {
            s.setSdate(null);
        }
        try {
            s.setEdate(rs.getDate(su+"edate"));
        } catch (SQLException e) {
            s.setEdate(null);
        }

        return s;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, UserModule s) {
        par.put("umid", s.getUmid());
        par.put("uid", s.getUid());
        par.put("mid", s.getMid());
        par.put("sdate",s.getSdate());
        par.put("edate",s.getEdate());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, UserModule s) {
        args.put("umid",s.getUmid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final UserModule s) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(s),
                        new String[] { "umid" });
                parsePS(ps, s);
                return ps;
            }

        };
    }

    private static String parseSQL(UserModule s) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into userapp_user_module(");
        int count = 0;
        if (null != s.getUmid()) {
            sql.append("umid,");
            count++;
        }
        if (null != s.getUid()) {
            sql.append("uid,");
            count++;
        }
        if (null != s.getMid()) {
            sql.append("mid,");
            count++;
        }
        if (null !=s.getSdate()) {
            sql.append("sdate,");
            count++;
        }
        if (null !=s.getEdate()) {
            sql.append("edate,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, UserModule s)
        throws SQLException {
        int count = 1;
        if (null != s.getUmid()) {
            ps.setLong(count++,s.getUmid());
        }
        if (null != s.getUid()) {
            ps.setLong(count++,s.getUid());
        }
        if (null != s.getMid()) {
            ps.setLong(count++,s.getMid());
        }
        if (null != s.getSdate()) {
            ps.setDate(count++, (Date) s.getSdate());
        }
        if (null != s.getEdate()) {
            ps.setDate(count++, (Date) s.getEdate());
        }

    }
}
